//
//  RwdUserInfosView.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/13.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage


let margin: CGFloat = 10

class RwdUserInfosView: UIView {
    //记录约束
    var bottomConstraint : Constraint?
    
    var statusViewModel: RwdStatusViewModel?{
        
        didSet{
            //设置头像
            if let headViewUrl = statusViewModel?.status?.user!.profile_image_url {
                
                headView.sd_setImageWithURL(NSURL(string: headViewUrl), placeholderImage: UIImage(named: "avatar_default_big"))
           
            }
            //设 置昵称
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            //设置微博内容
            contentLabel.text = statusViewModel?.status?.text
            //会员等级
            mbshipView.image = statusViewModel?.mbRankImage
            //会员分类
            verifiedTypeLevel.image = statusViewModel?.verified_typeImage
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
            
            bottomConstraint?.uninstall()
            
            //原创微博的配图
            let count = statusViewModel?.status?.pic_urls?.count
                if count > 0 {
                pictureView.pictureUrls = statusViewModel?.status?.pic_urls
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
                })
                
            }else {

                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(margin).constraint
                })
}
        }
        
    }
    
    //懒加载控件,然后设置约束, 添加到view上, 作为cell最上方的视图
    //头像
    private lazy var headView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    private lazy var nameLabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
    //认证类型等级图片
    private lazy var verifiedTypeLevel = UIImageView(image: UIImage(named: "avatar_vip"))
    //会员等级图片
    private lazy var mbshipView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //发布时间
    private lazy var timelabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 14)
    //来源label
    private lazy var sourceLabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 14)
    //发布的内容label
    private lazy var contentLabel: UILabel = {
        let lebel = UILabel(textColor: UIColor.blackColor(), fontSize: 15)
        
            lebel.numberOfLines = 0
        
            return lebel
            }()
    //添加配图的collectionview
    private lazy var pictureView: RwdPictureView = {
        let view = RwdPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)

        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setUpUI() {
        //添加子控件
        addSubview(headView)
        addSubview(nameLabel)
        addSubview(verifiedTypeLevel)
        addSubview(mbshipView)
        addSubview(timelabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        

        //添加约束
        headView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(margin)
            make.top.equalTo(self).offset(margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeLevel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headView.snp_trailing)
            make.centerY.equalTo(headView.snp_bottom)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headView.snp_trailing).offset(margin)
            make.top.equalTo(headView)
        }
        mbshipView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(margin)
            make.top.equalTo(nameLabel)
        }
        timelabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headView.snp_trailing).offset(margin)
            make.bottom.equalTo(headView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timelabel.snp_trailing).offset(margin)
            make.bottom.equalTo(timelabel.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headView.snp_bottom).offset(margin)
            make.leading.equalTo(headView)
            make.trailing.equalTo(-margin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            make.leading.equalTo(contentLabel.snp_leading)
        }
        
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(margin).constraint
        }
        

       
    }
    
    
}
